<template>
	<view class="invite">
		<view class="share-post">
			<image :src="img_source" mode="heightFix" style="height: 100%;"></image>
		</view>
		<view class="button-box">
			<button class="btn btn-primary btn-block" @click="download">保存到相册</button>
		</view>
	</view>
</template>

<script>
	//import api from '@/utils/api/ucenter/index.js';
	import api from "@/utils/api/invite/index.js"
	export default {
		data() {
			return {
				img_source: ''
			};
		},
		onLoad(e) {
			if(typeof(e.image) != "undefined"){
				this.img_source = e.image + "?" + Math.random();
			}else{
				this.getData();
			}
			
		},
		mounted() {

		},
		methods: {
			async getData() {
				var res = await api.sharePost();
				if (res.status == 1) {
					this.img_source = res.data.img_source + "?" + Math.random();
					console.log(this.img_source)
					uni.setNavigationBarTitle({
						title: "邀请码:" + res.data.invite_code
					})
				}
			},
			download() {
				var self = this;
				uni.downloadFile({
					url: this.img_source,
					success: res => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									uni.showToast({
										title: '保存成功',
										icon: 'none'
									});
								},
								fail: function() {
									uni.showToast({
										title: '保存失败，请稍后重试',
										icon: 'none'
									});
								}
							});
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	view {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
	}

	.invite {
		height: 100%;

		.share-post {
			height: 100%;
			padding: 15px;
			text-align: center;

			image {
				margin: 0 auto;
				border-radius: 6px;
				box-shadow: 0 0 15px rgba(0, 0, 0, .5);
			}
		}

		.button-box {
			height: 100px;
			padding: 15px;
			justify-content: center;
		}
	}
</style>
